package com.example.springstack.springsecurity.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springstack.springsecurity.entity.AuthenticationInfoVo;
import com.example.springstack.springsecurity.entity.AuthorizationUser;
import com.example.springstack.springsecurity.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Qiangfanghao
 */
@Controller
public class LoginController {

    @Autowired
    private LoginServiceImpl loginService;

    /**
     * 注解的异常可以使用springAdvice进行捕获
     * @return
     */
//    @Secured("ROLE_abc")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('TEST')")
    @GetMapping("/loginMain")
    public String loginMain(){
        return "测试";
    }

    @ResponseBody
    @PostMapping("/test/login")
    public AuthenticationInfoVo login(@RequestBody AuthorizationUser authorizationUser, HttpServletRequest request) {
        return loginService.login(authorizationUser,request);
    }

    @RequestMapping("/loginFailure")
    public String loginFailure(){
        return "redirect:loginError.html";
    }
}
